package com.prp.gwt.client.widgets.user.article;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.user.article.ArticleHistoryHandler;
import com.prp.gwt.client.vo.SubjectCategoryList;

public class CreateEntityEntryPanel extends VerticalPanel {

	private static CreateEntityEntryPanel instance;
	Label label = new Label("没有找到，我来添加...");
	public static CreateEntityEntryPanel getInstance(){
		if(instance == null)
			instance = new CreateEntityEntryPanel();
		return instance;
	}
	
	private CreateEntityEntryPanel() {
		addStyleName("createEntityEntry");
		label.addStyleName("createEntityEntry-label");
	}

	public void load(final SubjectCategoryList list) {
		clear();
		add(label);
		for (int i = 0; i < list.size(); i++) {
			final HTML item = new HTML("<a>"+list.get(i).getName()+"</a>");
			HorizontalPanel innerPanel = new HorizontalPanel();
			innerPanel.add(new Label(">"));
			innerPanel.add(item);
			add(innerPanel);
			innerPanel.addStyleName("createEntityEntry-item");
			item.setStylePrimaryName("createEntityEntry-text");
			final int j = i;
			item.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					History.newItem(ArticleHistoryHandler.PREFIX_CREATE_ENTITY
							+ list.get(j).getId());
				}
			});
			item.addMouseOverHandler(new MouseOverHandler(){
				@Override
				public void onMouseOver(MouseOverEvent event) {
					item.addStyleDependentName("over");
				}
			});
			item.addMouseOutHandler(new MouseOutHandler(){
				@Override
				public void onMouseOut(MouseOutEvent event) {
					item.removeStyleDependentName("over");
				}
			});
		}
	}

}
